const { request } = require('../../services/request')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    loginBannerBg: '../../assets/images/login-bannder-bg.png',
    userList: [1],
    foward: '', //目的menuid
    keyword: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    const { title } = option;
    if (option.menuId) {
      this.setData({ foward: option.menuId, title })
    }
    this.searchList()
  },
  handleDel() {},
  handleNewUser() {
    wx.navigateTo({
      url: '/pages/edit-user/index?type=new',
    })
  },
  handleChange(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  handleSearch() {
    request('POST', `/applet/answer/user/list?keyword=${ this.data.keyword}`).then(
      (res) => {
        this.setData({
          userList: res.data,
        })
      }
    )
  },
  searchList() {
    request('POST', '/applet/answer/user/list')
      .then((res) => {
        this.setData({
          userList: res.data,
        })
      })
      .catch()
  },
  selectUser(e) {
    const { answeUserId, userId } = e.detail
    wx.redirectTo({
      url: `/pages/channel/index?title=${this.data.title}&menuId=${this.data.foward}&userId=${userId}&answeUserId=${answeUserId}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.handleSearch()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
